package com.example.dingtalk.Impl;


import com.aliyun.dingtalkoauth2_1_0.Client;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import com.aliyun.teaopenapi.models.Config;
import com.example.dingtalk.DingTalkClient;

public class DingTalkClientImpl implements DingTalkClient {

    // 应用的AppKey和AppSecret，应从环境变量或配置文件中获取，避免硬编码在代码中
    private static final String APP_KEY = "dingfrogc9voed4jlizj";
    private static final String APP_SECRET = "UAqGdHyDLRSWqDGBJf3s07OrNgdM8UZBQp1vZMyhUX-nmgCOZ6PKzQWQCZaCuq1t";

    /**
     * 获取用户令牌
     * @param clientId 客户端ID
     * @param clientSecret 客户端密钥
     * @return 用户令牌
     * @throws Exception 如果获取过程中发生错误
     */
    @Override
    public String getUserToken(String clientId, String clientSecret) throws Exception {
        Config config = new Config();
        config.protocol = "https"; // 使用HTTPS协议
        config.regionId = "central";  // 设置地区ID
        Client client = new Client(config);

        GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
                .setClientId(clientId) // 设置客户端ID
                .setClientSecret(clientSecret) // 设置客户端密钥
                .setGrantType("client_credentials"); // 指定授权类型为客户端凭证

        // 调用API获取用户令牌
        GetUserTokenResponse response = client.getUserToken(getUserTokenRequest); // 发起获取用户令牌请求
        return response.body.accessToken;  // 返回访问令牌
    }
}